<?php

/************************************

EasyCB, Community Forum Software
Copyright (C) 2007  Jonathon D. Keogh <jonathon.keogh@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

$Id: page.boards.php 191 2008-05-12 17:06:00Z jonathon.keogh $

************************************/

// Get our functions
require "inc.func.php";

page_header(config_get("BoardName"));

// #21 bug fix - hide intro box if there isn't an introduction
if(config_get("BoardIntroduction") != "")
{
	page_section();
	print nl2br2(config_get("BoardIntroduction"));
	page_section();
}

$unread_posts = Array();
$u = db_query("SELECT * FROM `" . config_get("db_prefix") . "unread` WHERE `UserID`='" . db_escape(sess_get("UserID")) . "';");
if(db_num_rows($u) > 0)
{
	page_section();
	print "There " . (db_num_rows($u) == 1 ? "is one new post" : "are " . db_num_rows($u) . " new posts") . " awaiting your reading.";
	page_section();
	
	while($row = db_fetch_assoc($u))
	{
		$topic = $row['TopicID'];
		
		$board = db_query("SELECT * FROM `" . config_get("db_prefix") . "topics` WHERE `ID`='" . db_escape($topic) . "';");
		$board = db_fetch_assoc($board);
		$board = $board['Board'];
		
		$parent = explode('|', board_trail($board));
		$parent = $parent[1];
		
		$unread_posts[$parent] = true;
	}
}

page_section("Board list");
$boards = db_query("SELECT * FROM `" . config_get("db_prefix") . "boards` WHERE `Parent`='0' ORDER BY `Order` ASC;");
while($board = db_fetch_assoc($boards))
{
	print "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">\n";
	print "<tr>\n";
	print "<td colspan=\"4\"><b>" . htmlentities($board['Label']) . "</b></td>\n";
	print "</tr>\n";
	
	$subboards = db_query("SELECT * FROM `" . config_get("db_prefix") . "boards` WHERE `Parent`='" . db_escape($board['ID']) . "' ORDER BY `Order` ASC;");
	while($subboard = db_fetch_assoc($subboards))
	{
		print "<tr>\n";
		print "<td rowspan=\"2\" width=\"50\" valign=\"middle\" align=\"center\"><img src=\"img/" . (isset($unread_posts[$subboard['ID']]) ? "new-posts-section.jpg" : "blank-section.jpg") . "\" alt=\"X\"></td>\n";
		print "<td><a href=\"page.board.php?id=" . urlencode($subboard['ID']) . "\">" . htmlentities($subboard['Label']) . "</a></td>\n";
		print "<td rowspan=\"2\" valign=\"middle\" align=\"center\" width=\"100\">{$subboard['TopicCount']} topic" . ($subboard['TopicCount'] == 1 ? "" : "s") . "<br>{$subboard['PostCount']} post" . ($subboard['PostCount'] == 1 ? "" : "s") . "</td>\n";
		print "<td rowspan=\"2\" valign=\"middle\" align=\"center\" width=\"200\">" . board_lastPost($subboard['LastPost']) . "</td>\n";
		print "</tr>\n";
		print "<tr>\n";
		print "<td>" . htmlentities($subboard['Description']) . "</td>\n";
		print "</tr>\n";
	}
	
	print "</table>\n";
}
page_section();

page_section();
?>

These boards hold <b><?=config_get("TopicCount") ?></b> topic<?=(config_get("TopicCount") == 1 ? "" : "s") ?>, <b><?=config_get("PostCount") ?></b> post<?=(config_get("PostCount") == 1 ? "" : "s") ?> and <b><?=config_get("UserCount") ?></b> registered user<?=(config_get("UserCount") == 1 ? "" : "s") ?>.<br>
<br>
Users online:<br>
<?php

$online = db_query("SELECT `UserID` FROM `" . config_get("db_prefix") . "sessions` WHERE `LastTime` >= " . (time() - (60 * 5)) . " AND `UserID` != 0;");
$randomers = db_query("SELECT * FROM `" . config_get("db_prefix") . "sessions` WHERE `LastTime` >= " . (time() - (60 * 5)) . " AND `UserID`=0;");
$randomers = db_num_rows($randomers);
$total = db_num_rows($online);
$current = 0;
while($row = db_fetch_assoc($online))
{
	$current++;
	
	print "<a href=\"page.user.php?id=" . urlencode($row['UserID']) . "\">" . htmlentities(user_name($row['UserID'])) . "</a>";
	
	if($total != 1)
	{
		if($randomers == 0 && $current == $total-1)
		{
			print " and ";
		} elseif($current != $total) {
			print ", ";
		}
	}
	
	if($randomers != 0 && $current == $total)
	{
		print " and $randomers randomer" . ($randomers == 1 ? "" : "s");
	}
}

if($total == 0)
{
	print "$randomers randomer" . ($randomers == 1 ? "" : "s");
}

?></p>

<?php
page_section();

page_footer('$Id: page.boards.php 191 2008-05-12 17:06:00Z jonathon.keogh $');

?>